// pages/buy/index.js
import { request } from '../../request/index'
Page({
  min: function () {
    if (this.data.min > this.data.buy.min) {
      this.setData({
        sum: this.data.sum - this.data.price,
        min: --this.data.min
      })
    }
  },
  max: function () {
    this.setData({
      sum: this.data.sum + this.data.price,
      min: ++this.data.min
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  buy: function () {
    var app = getApp()
    var data = {
      userId: app.globalData.userId,
      name: this.data.buy.name,
      termId: this.data.buy.id,
      coachId: this.data.buy.userId,
      price: this.data.buy.price,
      number: this.data.min,
      sum: this.data.sum,
      placeId: this.data.places[this.data.index].id,
    }
    // console.log(data)
    if (app.globalData.userId === this.data.buy.userId) {
      wx.showToast({
        title: '不可以购买自己的课程',
        icon: 'success',
        duration: 2000,
        mask: true,
        complete: function () {
          var timeId = setTimeout(() => {
            clearTimeout(timeId)
            wx.navigateBack({
              delta: 1
            })
          }, 2000);
        }
      })
    } else {
      request({ url: app.globalData.root + 'api/addOrder', method: 'POST', dataType: 'json', data: data }).then((res) => {
        wx.showToast({
          title: '购买成功',
          icon: 'success',
          duration: 2000,
          mask: true,
          complete: function () {
            var timeId = setTimeout(() => {
              clearTimeout(timeId)
              wx.navigateBack({
                delta: 1
              })
            }, 2000);
          }
        })
      })
    }

  },
  /**
   * 页面的初始数据
   */
  data: {
    array: [],
    objectArray: [
      // {
      //   id: 0,
      //   name: '北京健身房'
      // },
      // {
      //   id: 1,
      //   name: '上海健身房'
      // },
      // {
      //   id: 2,
      //   name: '深圳健身房'
      // },
      // {
      //   id: 3,
      //   name: '广州健身房'
      // }
    ],
    index: 0,
    buy: {
      // name: '增肌max',
      // person: '薪子',
      // avatar: 'http://129.28.168.85/img/1.png',
      // price: 300,
      // min: 8,
    },
    places: [],
    sum: 0,
    price: 0,
    min: 0,
    root: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var app = getApp()
    var temp = JSON.parse(options.item)
    request({ url: app.globalData.root + 'api/getUser', method: 'GET', dataType: 'json', data: { id: temp.userId } }).then((res) => {
      console.log(res)
      var tempBuy = temp
      tempBuy.avatar = res.data[0].avatar
      tempBuy.person = res.data[0].userName
      this.setData({
        buy: tempBuy,
        sum: temp.price * temp.min,
        price: temp.price,
        min: temp.min,
        root: app.globalData.root
      })
    })

    request({ url: app.globalData.root + 'api/placesFind', method: 'GET', dataType: 'json' }).then((res) => {
      console.log(res)
      var arr = []
      var arr1 = []
      res.data.forEach((item, index) => {
        arr.push(item.name)
        arr1.push({ id: index, name: item.name })
      })
      this.setData({
        array: arr,
        objectArray: arr1,
        places: res.data
      })
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})